package com.yuyou.train.user.service.impl;

import java.util.Date;
import java.util.List;

import com.yuyou.train.user.vo.resp.RespUserPointsTop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.service.api.user.entity.UserPoints;
import com.yuyou.train.user.mapper.UserPointsMapper;
import com.yuyou.train.user.service.IUserPointsService;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class UserPointsServiceImpl implements IUserPointsService{

	@Autowired
	private UserPointsMapper userPointsMapper;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userPointsMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(UserPoints record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return userPointsMapper.insert(record);
	}

	@Override
	public UserPoints selectByPrimaryKey(String id) {
		return userPointsMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserPoints record) {
		return userPointsMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int sumPointsByUserId(String userId, String action) {
		Integer points = userPointsMapper.sumPointsByUserId(userId, action);
		return (null == points ? 0 : points);
	}

	@Override
	public List<RespUserPointsTop> findUserPointsTop(String classId) {
		return userPointsMapper.findUserPointsTop(classId);
	}

	@Override
	public Integer myRank(String classId, String userId) {
		Integer myRank = userPointsMapper.myRank(classId, userId);
		if(null == myRank){
			Integer userTotal = userPointsMapper.countUserOfPoints(classId);
			myRank = userTotal + 1;
		}
		return myRank;
	}

	@Override
	public ResBoolSimpleInfo<String> addPoints(String userId, String action,
			String remark,int points) {

		UserPoints record=new UserPoints();
		record.setUserId(userId);
		record.setAction(action);
		record.setPoints(points);
		record.setRemark(remark);
		this.insert(record);
		return ResBoolSimpleInfo.getSuccessInfo();
	}
}
